<template>
  <div v-loading="loading" class="main">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="page" class="demo-form-inline" size="mini" style="margin-top: 20px">
      <el-form-item label="请求状态">
        <el-select v-model="page.params.logStatus" placeholder="请求状态" clearable>
          <el-option label="正常" :value="1" />
          <el-option label="异常" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="请求方法">
        <el-select v-model="page.params.logMethod" placeholder="请求方法" clearable>
          <el-option label="GET" value="GET" />
          <el-option label="POST" value="POST" />
          <el-option label="PUT" value="PUT" />
          <el-option label="DELETE" value="DELETE" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" sizi="mini" @click="getByPage">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 我是分割线 -->
    <!-- <el-divider /> -->
    <el-button type="danger" size="mini" class="add-btn" @click="handleBatchDelete">批量删除</el-button>
    <el-button type="primary" size="mini" class="add-btn" @click="exportAll">全部导出</el-button>
    <el-table :data="page.list" border style="width: 100%" @sort-change="changeSort" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="logId" label="编号" />
      <el-table-column prop="logUrl" label="请求链接" show-overflow-tooltip />
      <el-table-column prop="logParams" label="请求参数" show-overflow-tooltip />
      <el-table-column prop="logStatus" label="请求状态" width="70" sortable="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.logStatus === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="logMessage" label="异常信息" width="120" show-overflow-tooltip />
      <el-table-column prop="logMethod" label="请求方法" width="100" />
      <el-table-column prop="logTime" label="响应耗时(毫秒)" width="120" sortable="custom" />
      <el-table-column prop="logResult" label="请求结果" width="100" show-overflow-tooltip />
      <el-table-column prop="logIp" label="请求IP" />
      <el-table-column prop="createdTime" label="创建时间" width="160" sortable="custom" />
    </el-table>

    <el-pagination
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

  </div>
</template>

<script>
import logApi from '@/api/log'
export default {
  data() {
    return {
      selectionObjs: [],
      loading: false, // 控制是否显示加载效果
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [], // 数据
        sortColumn: 'created_time', // 排序列
        sortMethod: 'desc' // 排序方式
      }
    }
  },
  created() {
    this.getByPage()
  },
  methods: {
    getByPage() {
      this.page.list = null
      logApi.getByPage(this.page).then(resp => {
        console.log(resp.data)
        this.page = resp.data
      })
    },
    handleBatchDelete() {
      if (this.selectionObjs.length === 0) {
        return
      }
      // 删除
      this.$confirm('删除后将不可回复，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        this.selectionObjs.forEach(u => {
          ids.push(u.logId)
        })
        logApi.deleteByIds(ids).then(resp => {
          this.$message.success(resp.msg)
          this.getByPage()
        })
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getByPage()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getByPage()
    },
    changeSort(e) {
      if (e.order) {
        this.page.sortColumn = e.prop
        this.page.sortMethod = e.order
      } else {
        this.page.sortColumn = 'created_time'
        this.page.sortMethod = 'desc'
      }
      this.getByPage()
    },
    handleSelectionChange(val) {
      this.selectionObjs = val
    },
    exportAll() {
      this.loading = true
      logApi.exportExcel().then(res => {
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
        const elink = document.createElement('a')
        elink.download = '系统日志.xlsx'
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href)
        document.body.removeChild(elink)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style scoped>
  .add-btn {
    margin: 5px 0px 5px 0px;
  }
</style>
